package com.inossem.warehousemanage_tv.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TextView;

import com.inossem.warehousemanage_tv.R;

/**
 * 仓库概览
 *
 * @author 詹建宇
 * @time on 2019/2/13 11:24
 * @email jianyu.zhan@inossem.com
 */
public class WareFragment extends BaseFragment implements View.OnClickListener {

    private TextView[] tvs;
    private Integer position = 0;//当前点击的是哪个页面

    @Override
    public int initLayout() {
        return R.layout.fragment_ware;
    }

    @Override
    public void initView() {
        TextView page1Tv = mView.findViewById(R.id.page1Tv);
        TextView page2Tv = mView.findViewById(R.id.page2Tv);
        TextView page3Tv = mView.findViewById(R.id.page3Tv);
        TextView page4Tv = mView.findViewById(R.id.page4Tv);
        tvs = new TextView[]{page1Tv, page2Tv, page3Tv, page4Tv};
    }

    @Override
    public void initData() {
        startFragment(new WareMainFragment());
    }

    @Override
    public void initClick() {
        for (TextView tv : tvs) {
            tv.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.page1Tv:
                if (position == 0) return;
                startFragment(new WareMainFragment());
                break;
            case R.id.page2Tv:
                if (position == 1) return;
                startFragment(new WareShelvesFragment());
                break;
            case R.id.page3Tv:
                if (position == 2) return;
                startFragment(new WarePositionsFragment());
                break;
            case R.id.page4Tv:
                if (position == 3) return;
                startFragment(new WareMatsFragment());
                break;
        }
    }

    private void changeTextView() {
        for (int i = 0; i < tvs.length; i++) {
            TextView tv = tvs[i];
            String txt = tv.getText().toString().replace(">", "");

            switch (i) {
                case 1:
                    txt = mActivity.locationName;
                    break;
                case 2:
                    txt = mActivity.shelveName;
                    break;
            }

            if (i < position) {
                tv.setText(txt + ">");
                tv.setTextColor(getResources().getColor(R.color.base_font));
                tv.setVisibility(View.VISIBLE);
            } else if (i == position) {
                tv.setText(txt);
                tv.setTextColor(getResources().getColor(R.color.base_blue));
                tv.setVisibility(View.VISIBLE);
            } else if (i > position) {
                tv.setText(txt);
                tv.setTextColor(getResources().getColor(R.color.base_font));
                tv.setVisibility(View.GONE);
            }
        }

        if (position == 3 && mActivity.isTemporary) {
            tvs[1].setVisibility(View.GONE);
            tvs[2].setVisibility(View.GONE);
        }

        if (mActivity.isTemporaryShelves) {
            tvs[2].setVisibility(View.GONE);
        }

    }

    public void startFragment(BaseFragment fragment) {

        position = null;

        if (fragment instanceof WareMainFragment) {
            position = 0;
        } else if (fragment instanceof WareShelvesFragment) {
            position = 1;
        } else if (fragment instanceof WarePositionsFragment) {
            position = 2;
        } else if (fragment instanceof WareMatsFragment) {
            position = 3;
        }

        if (null != position) changeTextView();

        FragmentManager fragmentManager = mActivity.getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.frameLayout, fragment).commit();

        mActivity.showFragment = fragment;
    }

}
